package com.caidan.search.spider;

import com.caidan.model.domain.result.WebResult;
import com.caidan.search.utils.WebResultClearUtils;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

@Slf4j
public class BingSearchSpider {

    private static final String BASE_URL = "https://www.bing.com/search";
    private static final Map<String, String> headers = new HashMap<>();
    private static final Map<String, String> cookies = new HashMap<>();

    static {
        // 设置请求头信息
        headers.put("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
        headers.put("accept-language", "zh-CN,zh;q=0.9");
        headers.put("cache-control", "no-cache");
        headers.put("dnt", "1");
        headers.put("ect", "4g");
        headers.put("pragma", "no-cache");
        headers.put("priority", "u=0, i");
        headers.put("referer", "https://www.bing.com/");
        headers.put("sec-ch-ua", "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"");
        headers.put("sec-ch-ua-arch", "\"x86\"");
        headers.put("sec-ch-ua-bitness", "\"64\"");
        headers.put("sec-ch-ua-full-version", "\"131.0.6778.205\"");
        headers.put("sec-ch-ua-full-version-list", "\"Google Chrome\";v=\"131.0.6778.205\", \"Chromium\";v=\"131.0.6778.205\", \"Not_A Brand\";v=\"24.0.0.0\"");
        headers.put("sec-ch-ua-mobile", "?0");
        headers.put("sec-ch-ua-model", "\"\"");
        headers.put("sec-ch-ua-platform", "\"Windows\"");
        headers.put("sec-ch-ua-platform-version", "\"14.0.0\"");
        headers.put("sec-fetch-dest", "document");
        headers.put("sec-fetch-mode", "navigate");
        headers.put("sec-fetch-site", "cross-site");
        headers.put("sec-fetch-user", "?1");
        headers.put("sec-gpc", "1");
        headers.put("upgrade-insecure-requests", "1");
        headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36");
        // 设置cookie
        // 设置Cookies
        cookies.put("_EDGE_V", "1");
        cookies.put("MUID", "2070FF2B115E64001871E9BC1087654E");
        cookies.put("SRCHD", "AF=NOFORM");
        cookies.put("SRCHUID", "V=2&GUID=5C4AC65A120446BEBC2CC9FE21DF3C67&dmnchg=1");
        cookies.put("_UR", "QS=0&TQS=0&Pn=0");
        cookies.put("ANON", "A=4629D62CEA26CFA9CE93EA9DFFFFFFFF&E=1df0&W=1");
        cookies.put("PPLState", "1");
        cookies.put("NAP", "V=1.9&E=1dfb&C=WyJEvxy-05gaVVdZdlxJqBgwdyI0RJ78H40vEl_HFHrORd5DDbJ8qg&W=1");
        cookies.put("MMCASM", "ID=F2DB0F764A7F468395782A847480BB29");
        cookies.put("SnrOvr", "X=rebateson");
        cookies.put("MSCCSC", "1");
        cookies.put("imgv", "lodlg=1");
        cookies.put("BCP", "AD=1&AL=1&SM=1");
        cookies.put("_tarLang", "default=en");
        cookies.put("_TTSS_OUT", "hist=WyJ6aC1IYW5zIiwiZW4iXQ==");
        cookies.put("_TTSS_IN", "hist=WyJlbiIsIml0IiwiemgtSGFucyIsImF1dG8tZGV0ZWN0Il0=&isADRU=0");
        cookies.put("MSCC", "cid=43smkiif0hlctymh4wwsqta8-c1=2-c2=2-c3=2");
        cookies.put("KievRPSSecAuth", "FABSBBRaTOJILtFsMkpLVWSG6AN6C/svRwNmAAAEgAAACLySfixjA77aEARC7ophtpZcwvvtAXD8dhageWhWdDUltuT/h8Wof/8IamfdKWJzJQE2Kzfp3OauIb50NXcA0C6mUOAxCAxuYL05+L7DTs6gj/71JHXfiWIMnsGB1w68oK3M27B11p3bk/NAu0A0oYETTKB+N2AoHPRY/ZN4NqbF7Nwd/TgpcoriEa+Xoev6b2ULUbhWus1ZR6LBDBCn4lPxMbbFw4HeL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oi035ZKzmwviS+4n4cT8IMIhS3QgYoi1CKEhC9pcieKMHOk2G3pBfz354wC72JibsiXVhbNEptnOUuViUi6LmRYbMRCGrl/Zmz0DsOzQsMzRQAREi+kTcXeTbr+FP677PLzpX/1Eo=");
        cookies.put("_U", "1lTqxNMVJoLhYR-oCW8Iq7AF4Gh-rGG4MQy6Fq6jUrUuT8zKrUj6Ju0UXqN003oSjIEUsXuH9ECtj4ysBii8AVU8anWb5Umu3U86hujyTKOF4pvDVONGZKWCVG1Heou8vFmizP-Gh1kXM9P1c58KRwbG2LUujVRLs2qJrtqZxp1NPL18GlniKuLxdZdtClr0a9bYAPbEjUlpvSpYcHuJX5g");
        cookies.put("WLID", "UHo8p/4TuN87qe/Ft/HPvf5TwdTJwS5mKWy6FBXW61n8KiRcLvJtyDF4yd5bmcxk4HeRwbEF+eZYENwsFlTGThHsOuyoMZrH7LWrQNUZ2gg=");
        cookies.put("_HPVN", "CS=eyJQbiI6eyJDbiI6MTgsIlN0IjowLCJRcyI6MCwiUHJvZCI6IlAifSwiU2MiOnsiQ24iOjE4LCJTdCI6MCwiUXMiOjAsIlByb2QiOiJIIn0sIlF6Ijp7IkNuIjoxOCwiU3QiOjAsIlFzIjowLCJQcm9kIjoiVCJ9LCJBcCI6dHJ1ZSwiTXV0ZSI6dHJ1ZSwiTGFkIjoiMjAyNC0xMi0xOVQwMDowMDowMFoiLCJJb3RkIjowLCJHd2IiOjAsIlRucyI6MCwiRGZ0IjpudWxsLCJNdnMiOjAsIkZsdCI6MCwiSW1wIjo0NCwiVG9ubiI6MH0=");
        cookies.put("WLS", "C=f81d819ef44a51d4&N=san");
        cookies.put("_Rwho", "u=d&ts=2024-12-28");
        cookies.put("_EDGE_S", "SID=3AC8E10BBFFF60600EB9F468BE796171&mkt=en-us");
        cookies.put("ak_bmsc", "7603CD20F28229136396D59F1BCED071~000000000000000000000000000000~YAAQHmM4F48O6AiUAQAAPSIpDRr2r9Bhve5VPWFOdqEPnFo83v4dyhQ1HJAkx+tAAOP0P9IQ7YkFIaGIDiYa2MIAzOXE+TYOHB+51T5Zd8tN4Ocg9Vv+6AKRbvsITv3wh4mXhxkHAlvmb/pEAlXl68E32z7ZMHi010HYMo7Wt0/5UqlL5c7sOC4QL41fYUM7ACQ2lreCvr25iO3Wq/sImnPxLquKgDC+i0mkvacgNvFIKdTzpfUd6l+UKvlDvKIXXq09nSy7mOR4tQFa5xwP3fzM0t5NMa+8g2P+SNp/nspyzbC1OyEwsPJzle7byAEiwdpvS6seGz15eHkuoshaQqK9A42UwbC/Bf82gLm20OyIdc8d2ufxxsPYku2JsNYG2cJ2tQMpXE3GWQ==");
        cookies.put( "SRCHUSR", "DOB=20241002&T=1735390382000&TPC=1735381748000&POEX=W");
        cookies.put("USRLOC", "HS=1&ELOC=LAT=33.011234283447266|LON=114.0023193359375|N=%E9%A9%BF%E5%9F%8E%E5%8C%BA%EF%BC%8C%E6%B2%B3%E5%8D%97%E7%9C%81|ELT=10|");
        cookies.put("SRCHHPGUSR","SRCHLANG=zh-Hans&PV=15.3.1&BZA=0&PREFCOL=0&BRW=XW&BRH=S&CW=1920&CH=493&SCW=1905&SCH=2199&DPR=1.0&UTC=480&P=CfDJ8BJecyNyfxpMtsfDoM3OqQvYBko_t6h-415rRywMokoH0IkNyLf4ewUtm_f3ogvZe8ePxjkLL5CCNZaHY9KpAlysjUhz0vPoR_OOVxwPrmdirGkMvbl7UYdPhzThv0D0Lfu5KM7O3vkA4jonDKqmCqzAHOc95UIAXgRvdIeodBB12Hhn6zFPYsdwb5cIf0fIyA1&HV=1764221853&HVE=CfDJ8BJecyNyfxpMtsfDoM3OqQsnFKhzyoXe7ihF07L1_TtrylPKcLFq_SS8zrJ_qSiLB3a2Qss86Bt03kmtZz05Nj_Qu06DsV8gbLTtGXOJN4akk1wDtVQ5es5zaI-768U8zW9JUA2J6bQKRLUauOXMa1rQr5rkNNomVCTtNtN1OWs3OH52epxUvueZgyFGmS_1CA&PRVCW=1920&PRVCH=843&B=0&EXLTT=31&AV=14&ADV=14&RB=0&MB=0&IG=177BFA91CB204759AC568BD471F6A600");
        cookies.put("_RwBf", "r=1&ilt=383&ihpd=0&ispd=55&rc=20269&rb=20269&gb=0&rg=0&pc=20260&mtu=0&rbb=0.0&g=0&cid=&clo=0&v=10&l=2024-12-28T08:00:00.0000000Z&lft=0001-01-01T00:00:00.0000000&aof=0&ard=0001-01-01T00:00:00.0000000&rwdbt=1676857339&rwflt=1733878316&rwaul2=0&o=16&p=bingcopilotwaitlist&c=MY00IA&t=3983&s=2023-02-20T09:35:14.5946388+00:00&ts=2024-12-28T13:18:22.3302508+00:00&rwred=0&wls=2&wlb=0&wle=0&ccp=2&cpt=0&lka=0&lkt=0&aad=0&TH=&mta=0&e=vf8IhGEiUOCfWUezckFI3PUXvCtCTccjRJ4mwK7a1CjJE3qAtFURYdHjQIXU4nFnVQ659pIwELo3Nnc-P-SwdA&A=");
        cookies.put("_SS", "SID=3F6C8E7095D06D341DA998C194026C05&R=22116&RB=22116&GB=0&RG=0&RP=22113&PC=U316");
    };

    public Map<String, Object> searchBing(String query, Integer page) throws IOException {
        // 对查询参数进行URL编码
        String encodedQuery = URLEncoder.encode(query, "UTF-8");
        String urlWithParams = BASE_URL + "?q=" + encodedQuery + "&FORM=PERE&first=" + page;
        log.info("请求URL: {}", urlWithParams);  // 打印请求URL，便于调试
        HttpURLConnection connection = (HttpURLConnection) new URL(urlWithParams).openConnection();
        // 设置请求头
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            connection.setRequestProperty(entry.getKey(), entry.getValue());
        }
        // 设置cookies
        StringBuilder cookieString = new StringBuilder();
        for (Map.Entry<String, String> entry : cookies.entrySet()) {
            cookieString.append(entry.getKey()).append("=").append(entry.getValue()).append("; ");
        }
        connection.setRequestProperty("Cookie", cookieString.toString());

        // 发送请求
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);

        // 获取响应
        int status = connection.getResponseCode();
        if (status == 200) {
            // 解析HTML
            Document doc = Jsoup.connect(urlWithParams)
                    .headers(headers)
                    .cookies(cookies)
                    .get();

            System.out.println(doc.text());

            // 提取搜索结果
            Elements results = doc.select("li.b_algo");
            // 存储提取的搜索结果
            List<WebResult> resultList = new ArrayList<>();
            String interlocution = null; // 用于存储智能问答

//            for (Element result : results) {
//                // 获取标题和链接
//                String title = result.select(".b_title h2 a").text();
//                String link = result.select(".b_title h2 a").attr("href");
//                if (title.isEmpty()) {
//                    title = result.select(".tptxt .tptt").text();  // 尝试从其他地方获取标题
//                }
//
//                // 获取描述（优先获取长描述，若没有则获取短描述）
//                String description = result.select(".b_caption").select("p").text();
//
//                if (description.isEmpty()) {
//                    description = result.select(".b_lineclamp2").text();  // 获取短描述
//                }
//
//                // 确保在访问分割后的数组前检查它的长度
//                String finalDescription = "N/A";
//                if (!description.isEmpty()) {
//                    String[] descriptionParts = description.split("·");
//                    if (descriptionParts.length > 1) {
//                        finalDescription = descriptionParts[1];
//                    } else {
//                        finalDescription = descriptionParts[0];
//                    }
//                }
//                // 获取日期（尝试从 .news_dt 获取）
//                String date = result.select(".news_dt").text();
//
//                if (date.isEmpty()) {
//                    // 如果没有找到日期，尝试从描述中提取日期（如果存在）
//                    date = result.select(".b_caption p").text().split("·")[0];
//                }
//
//                // 获取图片链接（如果有）
//                String imageUrl = result.select(".rms_iac").attr("data-src");
//
//                if (imageUrl.isEmpty()) {
//                    imageUrl = result.select(".tpic .rms_iac").attr("data-src"); // 如果是其他位置
//                }
//                // 创建 WebResult 对象并添加到结果列表
//                WebResult webResult = new WebResult(link,title, finalDescription, date, imageUrl);
//                if(WebResultClearUtils.clear(webResult)){
//                    resultList.add(webResult);
//                }
//            }
            for (Element result : results) {
                // 1. 提取标题（兼容两种结构：h2链接 或 tptt文本）
                String title = result.select("h2 a").text();
                if (title.isEmpty()) {
                    title = result.select(".tptxt .tptt").text();
                }

                // 2. 提取链接（兼容h2链接 或 tilk类链接）
                String link = result.select("h2 a").attr("href");
                if (link.isEmpty()) {
                    link = result.select("a.tilk").attr("href");
                }

                // 3. 提取描述（优先b_caption下的p，兼容b_lineclamp2）
                String description = result.select(".b_caption p").text();
                if (description.isEmpty()) {
                    description = result.select(".b_lineclamp2").text();
                }

                // 处理描述（分割日期和内容，避免数组越界）
                String finalDescription = "N/A";
                String date = "N/A";
                if (!description.isEmpty()) {
                    String[] descParts = description.split("·");
                    if (descParts.length > 1) {
                        date = descParts[0].trim(); // 提取日期部分
                        finalDescription = descParts[1].trim(); // 提取描述内容
                    } else {
                        finalDescription = description.trim(); // 无日期时直接用描述
                    }
                }

                // 4. 提取图片URL（兼容rms_img的src属性）
                String imageUrl = result.select(".tpic img.rms_img").attr("src");
                if (imageUrl.isEmpty()) {
                    imageUrl = result.select(".rms_iac").attr("data-src"); // 备用图片源
                }

                // 5. 创建WebResult并过滤无效结果
                WebResult webResult = new WebResult(link, title, finalDescription, date, imageUrl);
                if (WebResultClearUtils.clear(webResult)) {
                    resultList.add(webResult);
                }
            }
            // 提取智能问答部分
            Element qnaDiv = doc.selectFirst("div.qna-mf"); // 查找智能问答
            if (qnaDiv != null) {
                interlocution = qnaDiv.text(); // 获取智能问答的内容
                log.error("智能问答: {}", interlocution);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("smart", interlocution);
            map.put("res", resultList);
            return map;
//            // 输出结果
//            for (WebResult result : resultList) {
//                System.out.println(result);
//            }
        } else {
            log.error("Failed to fetch page: {}", status);
            return Maps.newHashMap();
        }
    }
//    public static void main(String[] args) {
//        try {
//            String query = "春天来了春天来了是什么";  // 可以替换为需要的查询词
//            searchBing(query,1);
//        } catch (IOException e) {
//            System.out.println("Error: " + e.getMessage());
//        }
//    }
}
